/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import leven from "./index.js";
import er from './easy-replace.js';
import { TEN_kB, ONE_KB, ONE_MB } from './data.js';

// Comparison string
function leven_Test(){
    let TextString = TEN_kB;
    let messaged = ONE_KB;

    let Comparison;
    let startTime = Date.now();
    for (let i = 0; i < 5; i++) {
      Comparison = leven(TextString, messaged)
    }
    let endTime = Date.now();

    if(Comparison == null){
      throw new Error('Easyrelpace_leven Error')
    }
    print(`Easyrelpace_leven: ${endTime - startTime} ms`)
}

// Character substitution
function er_Test(){
    let TextString = ONE_MB;
    let Text = "";
    let ReplaceString = "t";
    let StringResults = "Easyrelpace_er";

    let startTime = Date.now();
    for (let i = 0; i < 5; i++) {
      Text = er(
          TextString,
          {
            searchFor: ReplaceString,
          },
          StringResults
      );
    }
    let endTime = Date.now();  
    
    if(Text == TextString){
      throw new Error('Easyrelpace_er Error')
    } 
    print(`Easyrelpace_er: ${endTime - startTime} ms`)
}

function Switch() {
  leven_Test()
  er_Test()
}
let loopCountForPreheat = 1;
for (let i = 0; i < loopCountForPreheat; i++) {
  Switch()
}

ArkTools.waitAllJitCompileFinish();

Switch()
